//
//  MyViewController.m
//  CoreData3
//
//  Created by ibokan on 14-11-5.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "MyViewController.h"
#import "Student.h"
#import "Teacher.h"
#import "Student1.h"
#import "Teacher1.h"
#import "AppDelegate.h"
@interface MyViewController ()
- (IBAction)add:(UIButton *)sender;
- (IBAction)read:(UIButton *)sender;
- (IBAction)manyadd:(UIButton *)sender;
- (IBAction)manyread:(UIButton *)sender;
@property(strong,nonatomic)NSManagedObjectContext *managedObjectContext;
@end

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    self.managedObjectContext = delegate.managedObjectContext;
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)add:(UIButton *)sender {
    Teacher *t1 = (Teacher *)[NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:self.managedObjectContext];
    t1.name = @"张老师";
    Student *s1 = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.managedObjectContext];
    s1.name = @"李四";
    t1.student = s1;
    s1.teacher = t1;
    [self.managedObjectContext save:nil];
}

- (IBAction)read:(UIButton *)sender {
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Teacher"];
    Teacher *teacher = [self.managedObjectContext executeFetchRequest:request error:nil][0];
    NSLog(@"teacher name = %@",teacher.name);
    Student *student = teacher.student;
    NSLog(@"student name = %@",student.name);
}

- (IBAction)manyadd:(UIButton *)sender {
    Student1 *s1 = (Student1 *)[NSEntityDescription insertNewObjectForEntityForName:@"Student1" inManagedObjectContext:self.managedObjectContext];
    Student1 *s2 = (Student1 *)[NSEntityDescription insertNewObjectForEntityForName:@"Student1" inManagedObjectContext:self.managedObjectContext];
    Student1 *s3 = (Student1 *)[NSEntityDescription insertNewObjectForEntityForName:@"Student1" inManagedObjectContext:self.managedObjectContext];
    s1.name = @"张老三";
    s2.name = @"张大三";
    s3.name = @"张小三";
    Teacher1 *t1 = (Teacher1 *)[NSEntityDescription insertNewObjectForEntityForName:@"Teacher1" inManagedObjectContext:self.managedObjectContext];
    t1.name = @"张老师";
    s1.teacher = t1;
    s2.teacher = t1;
    s3.teacher = t1;
    
    [t1 addStudentsObject:s1];
    [t1 addStudentsObject:s2];
    [t1 addStudentsObject:s3];
    
    [self.managedObjectContext save:nil];
}

- (IBAction)manyread:(UIButton *)sender {
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Teacher1"];
    Teacher1 *teacher = [self.managedObjectContext executeFetchRequest:request error:nil][0];
    NSLog(@"%@",teacher.name);
    NSSet *students = teacher.students;
    NSLog(@"teacher name is %@",teacher.name);
    for (id obj in students) {
        NSLog(@"student name is %@",[obj name]);
    }
}































@end
